// Vite中文网：https://vitejs.cn/config/
import { ConfigEnv, loadEnv, UserConfig } from 'vite';
import { resolve } from 'path';
import uni from '@dcloudio/vite-plugin-uni';
import type { ProcessImportMetaEnv } from './src/types';

/**项目根目录 */
const root = process.cwd();
export default ({ mode }: ConfigEnv): UserConfig => {
    /**当前运行或打包环境 */
    const env = loadEnv(process.argv[2] === '--mode' ? process.argv[3] : mode, root) as Record<string, string> & ProcessImportMetaEnv;
    return {
        /**项目根目录 */
        root,

        /**开发或生产环境服务的公共基础路径 */
        base: './',

        /**定义全局常量替换方式 */
        define: {
            'process.env': {
                VITE_ENV: env.VITE_ENV,
                VITE_DEV: Boolean(env.VITE_DEV),
                VITE_PRO: Boolean(env.VITE_PRO),
                VITE_TEST: Boolean(env.VITE_TEST),
                VITE_APP_TITLE: env.VITE_APP_TITLE,
                VITE_APP_CACHE_PREFIX: env.VITE_APP_CACHE_PREFIX,
            } as ProcessImportMetaEnv,
        },

        /**设置路径别名 */
        resolve: {
            alias: [
                {
                    find: /@\//,
                    replacement: resolve(root, '.', 'src/'),
                },
            ],
            extensions: ['.js', '.jsx', '.cjs', '.ts', '.tsx', '.cts', '.css', '.less', '.json'], // 使用路径别名时想要省略的后缀名，可以自己 增减
        },
        // 插件
        plugins: [uni()],
        // 开发服务器配置
        server: {
            host: true,
            // open: true,
            port: Number(env.VITE_SERVER_PORT) || 3000,
            proxy: {
                '/api': {
                    target: env.VITE_PROXY_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
                '/upload': {
                    target: env.VITE_PROXY_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/upload/, ''),
                },
            },
        },
        // 构建配置
        build: {
            outDir: env.VITE_Build_OUT_DIR || 'dist',
            // eslint-disable-next-line no-extra-boolean-cast
            sourcemap: Boolean(env.VITE_SOURCEMAP) ? 'inline' : false,
            chunkSizeWarningLimit: 1500,
            terserOptions: {
                compress: {
                    drop_debugger: Boolean(env.VITE_DROP_DEBUGGER),
                    drop_console: Boolean(env.VITE_DROP_CONSOLE),
                },
            },
            rollupOptions: {
                output: {
                    entryFileNames: `assets/[name].${new Date().getTime()}.js`,
                    chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
                    assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
                    compact: true,
                    // manualChunks: {
                    //     vue: ['vue', 'vue-router', 'vuex'],
                    //     echarts: ['echarts'],
                    // },
                },
            },
        },
    };
};
